package ac.zlf.core.util;

import java.util.concurrent.atomic.AtomicInteger;

public class GeneratorUtil {
    private static final String str62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final AtomicInteger ATOM_INT_FOR14 = new AtomicInteger(0);
    private static final AtomicInteger ATOM_INT_FOR15 = new AtomicInteger(0);

    public static final String create15() {
        StringBuilder sb = new StringBuilder(15);
        sb.append(Long.toHexString(System.currentTimeMillis()));
        int current = ATOM_INT_FOR15.incrementAndGet();
        if (current > 1600000) {
            current %= 1600000;
            ATOM_INT_FOR15.set(current);
        }
        String str = longTo36(current);
        if (str.length() == 1) {
            sb.append("000").append(str);
        } else if (str.length() == 2) {
            sb.append("00").append(str);
        } else if (str.length() == 3) {
            sb.append("0").append(str);
        } else {
            sb.append(str);
        }
        return sb.toString();
    }

    public static final String create14() {
        StringBuilder sb = new StringBuilder(14);
        sb.append(Long.toHexString(System.currentTimeMillis()));
        int current = ATOM_INT_FOR14.incrementAndGet();
        if (current > 46655) {
            current %= 46656;
            ATOM_INT_FOR14.set(current);
        }
        String str = longTo36(current);
        if (str.length() == 1) {
            sb.append("00").append(str);
        } else if (str.length() == 2) {
            sb.append("0").append(str);
        } else {
            sb.append(str);
        }
        return sb.toString();
    }

    public static final String longTo36(long num) {
        return ten2Any(num, 36);
    }

    private static final String ten2Any(long num, int base) {
        StringBuilder sb = new StringBuilder(7);
        while (num != 0L) {
            sb.append("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt((int) (num % base)));
            num /= base;
        }
        return sb.reverse().toString();
    }
}